package com.atguigu.guli.service.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.atguigu.guli.service.edu.entity.ExcelSubjectData;
import com.atguigu.guli.service.edu.entity.Subject;
import com.atguigu.guli.service.edu.entity.vo.SubjectVo;
import com.atguigu.guli.service.edu.listener.ExcelSubjectDataListener;
import com.atguigu.guli.service.edu.mapper.SubjectMapper;
import com.atguigu.guli.service.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-07-01
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    //获取excel记录并逐行导入
    @Override
    public void batchImport(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelSubjectData.class,new ExcelSubjectDataListener(baseMapper)).excelType(ExcelTypeEnum.XLS).sheet().doRead();

    }

    @Override
    public List<SubjectVo> nestedList() {

        ArrayList<SubjectVo> subjectVoArrayList = new ArrayList<>();

        //查询所有数据
        List<SubjectVo> subjects = baseMapper.selectSubjectVoList();

        HashMap<String, SubjectVo> map = new HashMap<>();
        for (SubjectVo cur : subjects) {
            String id = cur.getId();
            map.put(id,cur);
        }

        for (SubjectVo cur : subjects) {
            String curParentid = cur.getParentId();
            if ("0".equals(curParentid)){
                //说明是顶级菜单节点，添加到subjectVoArrayList中
                subjectVoArrayList.add(cur);
            }else{
                //找到父亲节点，添加到他的儿子list中
                SubjectVo parentNode = map.get(curParentid);
                parentNode.getChildren().add(cur);
            }
        }
        return subjectVoArrayList;
    }


}
